var dynamicDomain = parent.dynamicDomain;
/** 收货人地区信息 */
var consigneeArea = {};
$("#codHelpUrl").attr("href", "http://help.jd.com/user/issue/103-983.html");
if(parent.$("#ui-dialog-close").size() != 0)
{
	if(parent.checkIsNewUser()||parent.$("#ui-dialog-close").val()==1){
		parent.$(".ui-dialog-close").css("display", "none");
		parent.$("#ui-dialog-close").val("");
	}
}
/**
 * 保存收货地址（包含保存常用人收货地址，根据id区分）
 */
function save_Consignee() {
	// 如果不隐藏重新获取用户填写的信息
	var consignee_id = null;
	var consignee_type = null;
	var consignee_provinceId = null;
	var consignee_cityId = null;
	var consignee_countyId = null;
	var consignee_townId = null;
	var consignee_name = null;
	var consignee_email = null;
	var consignee_address = null;
	var consignee_mobile = null;
	//yanwenqi 全球购添加身份证号
	var consignee_idCard = null;
	//end
	var consignee_phone = null;
	var consignee_provinceName = null;
	var consignee_cityName = null;
	var consignee_countyName = null;
	var consignee_townName = null;
	var consignee_ceshi1 = null;
	var isUpdateCommonAddress = 0;
	var consignee_commons_size = parent.$("#hidden_consignees_size").val();
	var giftSender_consignee_name   = "";
	var giftSender_consignee_mobile = "";
	var noteGiftSender = false;
	//地址别名
	var addressName =  null;
	 
	if(parent.isGiftBuy()){
		noteGiftSender= true;
		giftSender_consignee_name   = parent.$("#consigneeList_giftSenderConsigneeName").val();
		giftSender_consignee_mobile = parent.$("#consigneeList_giftSenderConsigneeMobile").val();		
	}
	// 如果展开之后需要添加常用地址列表
	isUpdateCommonAddress = 1;
	consignee_id = $("#consignee_form_id").val();
	
	if (parent.checkMaxConsigneeSize() && consignee_id == "") {
		parent.showMessageWarn("您的地址已经大于最大限制!");
		return;
	}

	consignee_type = $("#consignee_type").val();

	if (1 == consigneeArea.isAreaTab) {
		// 四级地址使用地区选择tab的情况
		consignee_provinceId = consigneeArea.provinceId;
		consignee_cityId = consigneeArea.cityId;
		consignee_countyId = consigneeArea.countyId;
		consignee_townId = consigneeArea.townId;
		consignee_provinceName = consigneeArea.provinceName;
		consignee_cityName = consigneeArea.cityName;
		consignee_countyName = consigneeArea.countyName;
		consignee_townName = consigneeArea.townName;
	} else {
		// 四级地址使用下拉菜单的情况
		consignee_provinceId = $("#consignee_province").find("option:selected").val();
		consignee_cityId = $("#consignee_city").find("option:selected").val();
		consignee_countyId = $("#consignee_county").find("option:selected").val();
		consignee_townId = $("#consignee_town").find("option:selected").val();
		consignee_provinceName = $("#consignee_province").find("option:selected").text();
		consignee_cityName = $("#consignee_city").find("option:selected").text();
		consignee_countyName = $("#consignee_county").find("option:selected").text();
		if (!$("#span_town").is(":hidden"))
			consignee_townName = $("#consignee_town").find("option:selected").text();
	}

	consignee_name = $("#consignee_name").val();
	consignee_address = $("#consignee_address").val();
	consignee_mobile = $("#consignee_mobile").val();
	//yanwenqi 全球购添加身份证号idCard
	consignee_idCard = $("#consignee_idCard").val();
	if(consignee_idCard == null || consignee_idCard == "undefined"){
		consignee_idCard = "";
	}
	//end
	consignee_phone = $("#consignee_phone").val();
	consignee_email = $("#consignee_email").val();
	consignee_ceshi1 = $("#consignee_ceshi1").val();

	if (consignee_email == null || consignee_email == "undefined")
		consignee_email = "";
	if (consignee_phone == null || consignee_phone == "undefined")
		consignee_phone = "";

	var checkConsignee = true;
	// 验证收货人信息是否正确
	if (!check_Consignee("name_div"))
		checkConsignee = false;
	
	// 验证地区是否正确
	// 四级地址使用地区选择tab的情况
	if (1 == consigneeArea.isAreaTab) {
		if (!check_Consignee("jd_area_tab"))
			checkConsignee = false;
    // 四级地址使用下拉菜单的情况
	} else {
		if (!check_Consignee("area_div"))
			checkConsignee = false;
	}
	// 验证收货人地址是否正确
	if (!check_Consignee("address_div"))
		checkConsignee = false;
	// 验证手机号码是否正确
	if (!check_Consignee("call_mobile_div"))
		checkConsignee = false;
	
	// yanwenqi 全球购增加身份证号 验证身份证号
	if (!check_Consignee("idCard_div"))
		checkConsignee = false;
	//end
	
	// 验证电话是否正确
	if (!check_Consignee("call_phone_div"))
		checkConsignee = false;
	// 验证邮箱是否正确
	if (!check_Consignee("email_div"))
		checkConsignee = false;
	//验证地址别名是否正确
	if (!check_Consignee("alias_div"))
		checkConsignee = false;

	if (!checkConsignee)
		return;
	var areaName = null;
	
	// 四级地址使用地区选择tab的情况
	if (1 == consigneeArea.isAreaTab) {
		areaName = consignee_provinceName + " " + consignee_cityName + " " + consignee_countyName + " " + consignee_townName;

    // 四级地址使用下拉菜单的情况
	} else {
		consignee_provinceName = consignee_provinceName.replace("*", "");
		consignee_cityName = consignee_cityName.replace("*", "");
		consignee_countyName = consignee_countyName.replace("*", "");
		areaName = consignee_provinceName + " " + consignee_cityName + " " + consignee_countyName + " ";
		if (consignee_townName != null && consignee_townName != "")
			areaName = areaName + consignee_townName.replace("*", "");
		// 如果使用常用联系人列表，则不对四级地址进行显示判断
		if (isEmpty(consignee_townId) || $("#span_town").is(":hidden"))
			consignee_townId = 0;
	}

	if (consignee_type == "")
		consignee_type = 1;
	addressName =$("#consignee_addressName").val();
	if(isEmpty(addressName)){
		addressName=consignee_name+" "+consignee_provinceName;
	}
	var param = "consigneeParam.id=" + consignee_id + "&consigneeParam.type=" + consignee_type 
		+ "&consigneeParam.name=" + consignee_name + "&consigneeParam.provinceId=" + consignee_provinceId 
		+ "&consigneeParam.cityId=" + consignee_cityId + "&consigneeParam.countyId=" + consignee_countyId 
		+ "&consigneeParam.townId=" + consignee_townId + "&consigneeParam.address=" + consignee_address 
		+ "&consigneeParam.mobile=" + consignee_mobile + "&consigneeParam.email=" + consignee_email 
		+ "&consigneeParam.phone=" + consignee_phone + "&consigneeParam.provinceName=" + consignee_provinceName 
		+ "&consigneeParam.cityName=" + consignee_cityName + "&consigneeParam.countyName=" + consignee_countyName 
		+ "&consigneeParam.townName=" + consignee_townName + "&consigneeParam.commonConsigneeSize=" + consignee_commons_size 
		+ "&consigneeParam.isUpdateCommonAddress=" + isUpdateCommonAddress + "&consigneeParam.giftSenderConsigneeName=" + giftSender_consignee_name 
		+ "&consigneeParam.giftSendeConsigneeMobile=" + giftSender_consignee_mobile + "&consigneeParam.noteGiftSender=" + noteGiftSender
		+ "&consignee_ceshi1="+consignee_ceshi1 + "&consigneeParam.idCard="+consignee_idCard+"&consigneeParam.addressName="+addressName;
	param = parent.addFlowTypeParam(param);
	var actionUrl = OrderAppConfig.DynamicDomain + "/consignee/saveConsignee.action";
	jQuery.ajax({
		type: "POST",
		dataType: "json",
		url: actionUrl,
		data: param,
		cache: false,
		success: function(consigneeResult, textStatus) {
			if (parent.isUserNotLogin(consigneeResult)) {
				parent.goToLogin();
				return;
			}
			if (consigneeResult.success) {
				parent.$("#beforePickName").val('');
				parent.$("#beforePickSiteId").val('');
				parent.$("#beforePickDate").val('');
				parent.$("#beforePickSiteNum").val('');
				parent.$("#beforePickRegionId").val('-1');
				parent.$("#beforePickSelRegionid").val('');
				var invoiceHtml = parent.$("#part-inv").html();
		        if (consigneeResult.restInvoiceByAddress == 22) {
		        	parent. $("#part-inv").html(invoiceHtml.replace("办公用品", "办公用品（附购物清单）"));
		        }
		        if (consigneeResult.restInvoiceByAddress == 2) {
		        	parent.$("#part-inv").html(invoiceHtml.replace("（附购物清单）", ""));
		        }
		        if (consigneeResult.supportElectro) {
		        	if(null != consigneeResult.restInvoiceCompanyName){
		        		parent.$("#part-inv").html("<span class='mr10'>普通发票（纸质）&nbsp; </span><span class='mr10'> "+consigneeResult.restInvoiceCompanyName+"&nbsp; </span><span class='mr10'>明细&nbsp; </span><a onclick='edit_Invoice()' class='ftx-05 invoice-edit' href='#none'>修改</a>");
		        	}else{
		        		parent.$("#part-inv").html("<span class='mr10'>普通发票（纸质）&nbsp; </span><span class='mr10'> 个人&nbsp; </span><span class='mr10'>明细&nbsp; </span><a onclick='edit_Invoice()' class='ftx-05 invoice-edit' href='#none'>修改</a>");
		        	}
		        }
		        if (consigneeResult.defaultElectro) {
		        	parent.$("#part-inv").html("<span class='mr10'>普通发票（电子）&nbsp; </span><span class='mr10'> 个人&nbsp; </span><span class='mr10'>明细&nbsp; </span><a onclick='edit_Invoice()' class='ftx-05 invoice-edit' href='#none'>修改</a>");
		        }
		        
				parent.$("#isNeedOpenInvoice").val(consigneeResult.openInvoice); // 隐藏域，判断修改地址后，是否需要修改发票信息，广州机构比较特殊
				if (consigneeResult.resultCode == "isRefreshArea") {
					show_ConsigneeDetail(consignee_id);
				} else {
					consignee_mobile = consigneeResult.consigneeShowView.mobile;
					var areaIds=consigneeResult.consigneeShowView.provinceId + "-" + consigneeResult.consigneeShowView.cityId + "-" + consigneeResult.consigneeShowView.countyId + "-" + consigneeResult.consigneeShowView.townId;
					// 弹出对应提示
					parent.$("#consignee-ret").html(consigneeResult.consigneeHtml);
					parent.$('#consignee_id').val(consigneeResult.consigneeShowView.id);
					parent.$('#hideAreaIds').val(areaIds);
					if (parent.isBigItemChange())
						parent.bigItemChangeArea();
					// 预售电话修改
					if (parent.$("#isPresale").val() == "true") {
						parent.$("#hiddenUserMobileByPresale").val(consignee_mobile);
						if (parent.$("#presaleMobile input").size() > 0) {
							parent.$("#presaleMobile input").val(consignee_mobile);
						} else if (parent.$("#userMobileByPresale").size() > 0) {
							parent.$("#userMobileByPresale").html(consignee_mobile);
						}
					}
					if (parent.hasTang9())
						parent.tang9ChangeArea();
					var commonConsigeeSize = parent.$("#hidden_consignees_size").val();
					var consigneeSize = parseInt(!commonConsigeeSize ? 0 : commonConsigeeSize);
					if (consignee_id == "") {
						consignee_id = parent.$("#consignee_id").val();
						var liHtml  =  "<li class='ui-switchable-panel' style='display: list-item;'  id='consignee_index_" + consignee_id + "' style='cursor: pointer;'>" +
											"<div class='consignee-item item-selected' consigneeId='" + consignee_id + "' clstag='pageclick|keycount|trade_201602181|1'>" +
												"<span limit='8'>" + addressName + "</span><b></b>" +
											"</div>" +
											"<div class='addr-detail'>" +
												"<span class='addr-name' limit='6'>" + consignee_name + "</span>" +
												"<span class='addr-info' limit='45'>" + areaName + consignee_address +"</span>" +
												"<span class='addr-tel'>" + consignee_mobile + "</span>" +
											"</div>" +
											"<div class='op-btns' consigneeId='" + consignee_id + "'>" +
												"<a href='#none' class='ftx-05 setdefault-consignee' fid='" + consignee_id + "'>设为默认地址</a>" +
												"<a href='#none' class='ftx-05 edit-consignee' fid='" + consignee_id + "'>编辑</a>" +
												"<a href='#none' class='ftx-05 del-consignee " + ((consigneeSize == 0 || consigneeSize == 1) ? " hide" : "") + "' fid='" + consignee_id + "'>删除</a>" +
											"</div>" +
										"</li>";
						//yanwenqi 全球购添加身份证号
						if(parent.$("#flowType").val() == 10){
							if(consignee_idCard == "undefined" || consignee_idCard == null || consignee_idCard == ""){
								liHtml  =  "<li class='ui-switchable-panel' style='display: list-item;'  id='consignee_index_" + consignee_id + "' style='cursor: pointer;'>" +
								"<div class='consignee-item item-selected' consigneeId='" + consignee_id + "' clstag='pageclick|keycount|trade_201602181|1'>" +
									"<span limit='8'>" + addressName + "</span><b></b>" +
								"</div>" +
								"<div class='addr-detail'>" +
									"<span class='addr-name' limit='6'>" + consignee_name + "</span>" +
									"<span class='addr-info' limit='32'>" + areaName + consignee_address +"</span>" +
									"<span class='addr-tel'>" + consignee_mobile + "</span>" +
									"<span class='addr-idCard'>未完善身份证信息</span>" +
								"</div>" +
								"<div class='op-btns' consigneeId='" + consignee_id + "'>" +
									"<a href='#none' class='ftx-05 setdefault-consignee' fid='" + consignee_id + "'>设为默认地址</a>" +
									"<a href='#none' class='ftx-05 edit-consignee' fid='" + consignee_id + "'>编辑</a>" +
									"<a href='#none' class='ftx-05 del-consignee " + ((consigneeSize == 0 || consigneeSize == 1) ? " hide" : "") + "' fid='" + consignee_id + "'>删除</a>" +
								"</div>" +
							"</li>";
							}
						}
						//end
						parent.$("#consignee-list .consignee-item").removeClass("item-selected");
						// update by lilong 产品经理zhaojingjing确认，如果当前页面有默认地址，新增地址在默认地址之后
						var first_li = parent.$(".consignee-item").parents("li").last();//当前列表第一项
//						var _tempstr = first_li.find("div span").first().html();
						 var _tempstr = first_li.find("div span").find(".addr-default").html();
						if(_tempstr && _tempstr.indexOf("默认地址") > -1) {
						  $(liHtml).insertAfter(first_li);// 1.插入在默认地址之后
						} else {
						  $(liHtml).insertBefore(first_li);// 2.插入在地址列表第一位
						}
						parent.$("#hidden_consignees_size").val("" + (consigneeSize = consigneeSize + 1));
						if (parent.$("#isNull").val() == "-1") {
							parent.goOrder();
						}
						if (consigneeSize > 1){
							var selectDiv = parent.$(".consignee-item").next().next();
							selectDiv.find(".del-consignee").removeClass("hide");
							selectDiv.find(".setdefault-consignee").removeClass("hide");
					        parent.$("#consigneeItemAllClick").addClass("hide");
					        parent.$("#consigneeItemHideClick").removeClass("hide");
						}
						if (parent.checkIsNewUser()) {
							parent.$("#isOpenConsignee").val("0");
						}
						//重新加载优惠券列表
						parent.reloadCoupon(consigneeResult.reloadCoupon);
						$("#del_consignee_type").val("0");
						parent.window.show_ConsigneeAll(1);
					} else {
						var defAddress = "<a href='#none' class='ftx-05 setdefault-consignee " + ((consigneeSize == 0 || consigneeSize == 1) ? " hide" : "") + "' fid='" + consignee_id + "'>设为默认地址</a>";
						if(parent.$("#consignee_index_"+consignee_id).find(".op-btns").find("a").size() == 2)
							defAddress = "<span></span>";
						var divHtml="<div class='consignee-item item-selected' consigneeId='" + consignee_id + "' clstag='pageclick|keycount|trade_201602181|1'>" +
										"<span limit='8'>" +addressName+ "</span><b></b>" +				
									"</div>" +
									"<div class='addr-detail'>" +
										"<span class='addr-name' limit='6'>" + consignee_name + "</span>" +
										"<span class='addr-info' limit='45'>" + areaName + consignee_address +"</span>" +
										"<span class='addr-tel'>" + consignee_mobile + "</span>" +
										(defAddress.indexOf("span")>-1?"<span class='addr-default'>" + "默认地址" + "</span>" :"") +
									"</div>" +
									"<div class='op-btns' consigneeId='" + consignee_id + "'>" + defAddress +
										"<a href='#none' class='ftx-05 edit-consignee' fid='" + consignee_id + "'>编辑</a>" +
										"<a href='#none' class='ftx-05 del-consignee " + ((consigneeSize == 0 || consigneeSize == 1) ? " hide" : "") + "' fid='" + consignee_id + "'>删除</a>" +
									"</div>";
						//yanwenqi 全球购添加身份证号
						if(parent.$("#flowType").val() == 10){
							if(consignee_idCard == "undefined" || consignee_idCard == null || consignee_idCard == ""){
								liHtml  =  "<li class='ui-switchable-panel' style='display: list-item;'  id='consignee_index_" + consignee_id + "' style='cursor: pointer;'>" +
								"<div class='consignee-item item-selected' consigneeId='" + consignee_id + "' clstag='pageclick|keycount|trade_201602181|1'>" +
									"<span limit='8'>" + addressName + "</span><b></b>" +
								"</div>" +
								"<div class='addr-detail'>" +
									"<span class='addr-name' limit='6'>" + consignee_name + "</span>" +
									"<span class='addr-info' limit='32'>" + areaName + consignee_address +"</span>" +
									"<span class='addr-tel'>" + consignee_mobile + "</span>" +
									"<span class='addr-idCard'>未完善身份证信息</span>" +
								"</div>" +
								"<div class='op-btns' consigneeId='" + consignee_id + "'>" +
									"<a href='#none' class='ftx-05 setdefault-consignee' fid='" + consignee_id + "'>设为默认地址</a>" +
									"<a href='#none' class='ftx-05 edit-consignee' fid='" + consignee_id + "'>编辑</a>" +
									"<a href='#none' class='ftx-05 del-consignee " + ((consigneeSize == 0 || consigneeSize == 1) ? " hide" : "") + "' fid='" + consignee_id + "'>删除</a>" +
								"</div>" +
							"</li>";
							}
						}
						//end
						parent.$("#consignee-list .consignee-item").removeClass("item-selected");
						if (consignee_id == "-1")
							parent.$(".consignee-item").first().html(divHtml);
						else
							parent.$("#consignee_index_" + consignee_id).html(divHtml);
					}
					// 新用户保存后将值写回
					parent.setResetFlag(0, '1');
					parent.save_Pay();
					parent.$("#isOpenConsignee").val("0");
					parent.$("#isRefreshArea").val("0");
					parent.subStrConsignee();
					parent.consigneeInfo();
					//重新加载优惠券列表
					parent.reloadCoupon(consigneeResult.reloadCoupon);
					
				}
				
			} else {
				if (consigneeResult.message != null && consigneeResult.message != ""){					
					parent.showMessageWarn(consigneeResult.message);
				}
			}
			//非新用户
			if(consignee_id != "" ){
				//地址别名修改埋点
				var addressNameBef = $("#consignee_addressName_old").val();
				if(addressName != addressNameBef){
					parent.log('changgouy', 'click',"jsydizhibieming");
				}
			}
			//yanwenqi 自提地址项目 编辑完之更改推荐自提
			parent.window.showRecommendSelfPickAddress();			
			  
			setTimeout(function(){
				window.location.href = "about:blank";
				parent.$.closeDialog();
			});
		},
		error: function(XMLHttpResponse) {
			parent.$.closeDialog();
		}
	});

}

/**
 * 编辑常用收货地址,展开对应信息
 */
function show_ConsigneeDetail(id) {
	// 隐藏20个数量的限制的提示
	parent.$("#addNumLimitNote").css("display", "none");
	var consignee_provinceName = $("#hidden_consignee_provinceName_" + id).val();
	var consignee_cityName = $("#hidden_consignee_cityName_" + id).val();
	var consignee_countyName = $("#hidden_consignee_countyName_" + id).val();
	var consignee_townName = $("#hidden_consignee_townName_" + id).val();
	var consignee_provinceId = $("#hidden_consignee_provinceId_" + id).val();
	var consignee_cityId = $("#hidden_consignee_cityId_" + id).val();
	var consignee_countyId = $("#hidden_consignee_countyId_" + id).val();
	var consignee_townId = $("#hidden_consignee_townId_" + id).val();
	// 展开三级地址
	$("#consignee_province").empty();
	$("#consignee_city").empty();
	$("#consignee_county").empty();
	$("#consignee_town").empty();
	$("#consignee_province").append("<option value='" + consignee_provinceId + "' selected >" + consignee_provinceName + "</option>");
	$("#consignee_city").append("<option value='" + consignee_cityId + "' selected >" + consignee_cityName + "</option>");
	$("#consignee_county").append("<option value='" + consignee_countyId + "' selected >" + consignee_countyName + "</option>");
	// 判断是否有四级地址，如果有则加载四级地址信息
	if (consignee_townId != null && consignee_townId != "0" && !isNaN(consignee_townId)) {
		$("#consignee_town").append("<option value='" + consignee_townId + "' selected >" + consignee_townName + "</option>");
		$("#span_town").show();
	}
	removeConsingeeMessage();
	// 异步加载四级地址
	loadAreaDetail(id, consignee_provinceId, consignee_cityId, consignee_countyId, consignee_townId);
}

/**
 * 编辑默认自提收货地址,展开对应信息
 */
function show_SelfPickConsigneeDetail(id) {
	// 隐藏20个数量的限制的提示
//	parent.$("#addNumLimitNote").css("display", "none");
	var consignee_provinceName = $("#hidden_consignee_provinceName_").val();
	var consignee_cityName = $("#hidden_consignee_cityName_").val();
	var consignee_countyName = $("#hidden_consignee_countyName_").val();
	var consignee_townName = $("#hidden_consignee_townName_").val();
	var consignee_provinceId = $("#hidden_consignee_provinceId_").val();
	var consignee_cityId = $("#hidden_consignee_cityId_").val();
	var consignee_countyId = $("#hidden_consignee_countyId_").val();
	var consignee_townId = $("#hidden_consignee_townId_").val();
	// 展开三级地址
	$("#consignee_province").empty();
	$("#consignee_city").empty();
	$("#consignee_county").empty();
	$("#consignee_town").empty();
	$("#consignee_province").append("<option value='" + consignee_provinceId + "' selected >" + consignee_provinceName + "</option>");
	$("#consignee_city").append("<option value='" + consignee_cityId + "' selected >" + consignee_cityName + "</option>");
	$("#consignee_county").append("<option value='" + consignee_countyId + "' selected >" + consignee_countyName + "</option>");
	// 判断是否有四级地址，如果有则加载四级地址信息
	if (consignee_townId != null && consignee_townId != "0" && !isNaN(consignee_townId)) {
		$("#consignee_town").append("<option value='" + consignee_townId + "' selected >" + consignee_townName + "</option>");
		$("#span_town").show();
	}
	removeConsingeeMessage();
	// 异步加载四级地址
	loadAreaDetail(id, consignee_provinceId, consignee_cityId, consignee_countyId, consignee_townId);
}

/**
 * 编辑常用收货地址,展开对应信息
 */
function showAreaTab(id,type) {
	// 隐藏20个数量的限制的提示
	parent.$("#addNumLimitNote").css("display", "none");
	var consignee_provinceId = null;
	var consignee_cityId = null;
	var consignee_countyId = null;
	var consignee_townId = null;
	if(type==1){
		consignee_provinceId = $("#hidden_consignee_provinceId_").val();
		consignee_cityId = $("#hidden_consignee_cityId_").val();
		consignee_countyId = $("#hidden_consignee_countyId_").val();
		consignee_townId = $("#hidden_consignee_townId_").val();
	}else{
		consignee_provinceId = $("#hidden_consignee_provinceId_" + id).val();
		consignee_cityId = $("#hidden_consignee_cityId_" + id).val();
		consignee_countyId = $("#hidden_consignee_countyId_" + id).val();
		consignee_townId = $("#hidden_consignee_townId_" + id).val();
	}
	// 加载地区选择标签
	loadAreaTab(consignee_provinceId, consignee_cityId, consignee_countyId, consignee_townId);
}

/**
 * 新增地址时加载默认地区选择tab
 */
function defaultAreaTab() {
	loadAreaTab('0', '0', '0', '0');
}

/**
 * 加载地区选择tab
 */
function loadAreaTab(provinceId, cityId, countyId, townId) {
	var initArea = provinceId + '-' + cityId + '-' + countyId + '-' + townId;
	seajs.use(['jdf/1.0.0/ui/switchable/1.0.0/switchable', 'jdf/1.0.0/ui/area/1.0.0/area'], function () {
		$('#jd_area').area({
	    	scopeLevel: 4,
			openMunicipality: true,
			initArea: initArea,
			defaultArea: null,
			isNotEmpty: false,
			repLevel: false,
	        className: {
	            text:'ui-area-text-wrap',
	            text_text:'ui-area-text',
	            content:'ui-area-content-wrap',
	            content_tab:'ui-area-tab',
	            content_content:'ui-area-content',
	            close:'ui-area-close'
	        },
	        switchable:{
	            callback:function(){
	                $('#jd_area').removeClass('has-scrollbar');
	                if ( $('#jd_area .ui-area-content-wrap').height() > 252 ) {
	                    $('#jd_area').addClass('has-scrollbar');
	                }
	            }
	        },
	        onReady: function() {
	        	var self = this;
	        	// 初始数据有误的情况
	        	if (initArea != '0-0-0-0' && self.options.initArea != self.el.data('select-local')) {
	        		$('#jd_area .ui-area-text').html('--请选择--').attr('data-id','0-0-0-0').attr('title', '--请选择--');
		        	consigneeArea.provinceId = 0;
		        	consigneeArea.cityId = 0;
		        	consigneeArea.countyId = 0;
		        	consigneeArea.isAreaTab = 1;

	        	} else {
	        		consigneeArea = arguments[0];
	        		consigneeArea.countyId = arguments[0].districtId;
	        		consigneeArea.countyName = arguments[0].districtName;
	        		consigneeArea.isAreaTab = 1;

	        		$("#areaNameTxt").text(consigneeArea.provinceName + consigneeArea.cityName + consigneeArea.countyName + consigneeArea.townName);
	        	}
	        },
	        onChange: function() {
	    		consigneeArea = arguments[1];
	        	consigneeArea.countyId = arguments[1].districtId;
	        	consigneeArea.countyName = arguments[1].districtName;
	        	consigneeArea.isAreaTab = 1;

	        	$("#areaNameTxt").text(consigneeArea.provinceName + consigneeArea.cityName + consigneeArea.countyName + consigneeArea.townName);

	        	tabIndex();
	        	
	        	//重新推荐自提
	        	if($("#isEditSelfPickHtml").length>0){
	        		doEditPickFourLevelAddress();
	        	}
	        }
		});
	});
}

/**
 * 加载四级地址，并选中默认值
 * 
 * @param provinceId
 * @param cityId
 * @param countyId
 * @param townId
 */
function loadAreaDetail(id, provinceId, cityId, countyId, townId) {
  var param = "consigneeParam.provinceId=" + provinceId + "&consigneeParam.cityId=" + cityId + "&consigneeParam.countyId=" + countyId + "&consigneeParam.townId=" + townId;
  var actionUrl = OrderAppConfig.DynamicDomain + "/consignee/loadAllAreas.action";
  jQuery.ajax({
    type: "POST",
    dataType: "text",
    url: actionUrl,
    data: param,
    async: true,
    cache: false,
    success: function(dataResult, textStatus) {
      // 没有登录跳登录
      if (parent.isUserNotLogin(dataResult)) {
        parent.goToLogin();
        return;
      }
      if (parent.isHasMessage(dataResult)) {
        var message = parent.getMessage(dataResult);
        parent.showMessageWarn(message);
      } else {
        $("#span_area").html(dataResult);
      }
    },
    error: function(XMLHttpResponse) {
      //parent.showMessageWarn("系统繁忙，请稍后再试！");
      return false;
    }
  });
}

/**
 * 验证收货地址消息提示
 * 
 * @param divId
 * @param value
 */
function check_Consignee(divId) {
	var errorFlag = false;
	var errorMessage = null;
	var value = null;
	var consignee_id = $("#consignee_form_id").val();
	
	var flowType = parent.$("#flowType").val();
	
	if(divId == "alias_div"){
		value = $("#consignee_addressName").val();
		if (isEmpty(value)) {
			//errorFlag = true;
			//errorMessage = "请您填写地址别名";
		}else if (value.length > 20) {
			errorFlag = true;
			errorMessage = "地址别名不能大于20位";
		}
		else if (!is_forbid(value)) {
			errorFlag = true;
			errorMessage = "地址别名中含有非法字符";
		}
	}
	// 验证收货人名称
	else if (divId == "name_div") {
		value = $("#consignee_name").val();
		if (isEmpty(value)) {
			errorFlag = true;
			errorMessage = "请您填写收货人姓名";
		}
		if(flowType == 10){
			if(!/^([\u4e00-\u9fa5])([\u4e00-\u9fa5·]){0,23}([\u4e00-\u9fa5])$/i.test(value)){
				errorFlag = true;
			}else{
				if(value.search(/·{2,}/) > -1){
					errorFlag = true;
				}
			}
			if(errorFlag){
				errorMessage = "收件人姓名仅支持简体中文，长度不超过25个字";
			}
		}else{
			if (value.length > 25) {
				errorFlag = true;
				errorMessage = "收货人姓名不能大于25位";
			}
			if (!is_forbid(value)) {
				errorFlag = true;
				errorMessage = "收货人姓名中含有非法字符";
			}
		}
	}
	// 验证邮箱格式
	else if (divId == "email_div") {
		value = $("#consignee_email").val();
		if (!isEmpty(value)) {
			if (value.length > 50) {
				errorFlag = true;
				errorMessage = "邮箱长度不能大于50位";
			}
			if (!check_email_new(value)) {
				errorFlag = true;
				errorMessage = "邮箱格式不正确";
			}
			if(consignee_id == ""){
				if(value.indexOf("*") > -1){
					errorFlag = true;
					errorMessage = "邮箱格式不正确";
				}
			}
		}
	}
	// 验证地区是否完整
	else if (divId == "area_div") {
		var provinceId = $("#consignee_province").find("option:selected").val();
		var cityId = $("#consignee_city").find("option:selected").val();
		var countyId = $("#consignee_county").find("option:selected").val();
		var townId = $("#consignee_town").find("option:selected").val();
		// 验证地区是否正确
		if (isEmpty(provinceId) || isEmpty(cityId) || isEmpty(countyId)
				|| ($("#span_town").html() != null && $("#span_town").html() != "" && !$("#span_town").is(":hidden") && isEmpty(townId))) {
			errorFlag = true;
			errorMessage = "请您填写完整的地区信息";
		}
	}
	// 验证地区是否完整
	else if (divId == "jd_area_tab") {
		var provinceId = consigneeArea.provinceId;
		var cityId = consigneeArea.cityId;
		var countyId = consigneeArea.countyId;
		var townId = consigneeArea.townId;
		// 验证地区是否正确
		if ("0" == provinceId || "0" == cityId || "0" == countyId
				|| 0 == provinceId || 0 == cityId || 0 == countyId) {
			errorFlag = true;
			errorMessage = "请您填写完整的地区信息";
		}
	}
	// 验证收货人地址
	else if (divId == "address_div") {
		value = $("#consignee_address").val();
		if (isEmpty(value)) {
			errorFlag = true;
			errorMessage = "请您填写收货人详细地址";
		}
		if (!is_forbid(value)) {
			errorFlag = true;
			errorMessage = "收货人详细地址中含有非法字符";
		}
		if (value.length > 50) {
			errorFlag = true;
			errorMessage = "收货人详细地址过长";
		}
	}
	// 验证手机号码
	else if (divId == "call_mobile_div") {
		value = $("#consignee_mobile").val();
		divId = "call_div";
		if (isEmpty(value)) {
			errorFlag = true;
			errorMessage = "请您填写收货人手机号码";
		} else {
			if (!check_mobile_new(value)) {
				errorFlag = true;
				errorMessage = "手机号码格式不正确";
			}
			if(consignee_id == "" && value.indexOf("*") > -1){
				errorFlag = true;
				errorMessage = "手机号码格式不正确";
			}
		}

		if (!errorFlag) {
			value = $("#consignee_phone").val();
			if (!isEmpty(value)) {
				if (!is_forbid_new(value)) {
					errorFlag = true;
					errorMessage = "固定电话号码中含有非法字符";
				}
				if (!checkPhoneNew($("#consignee_mobile").val(),value)) {
					errorFlag = true;
					errorMessage = "固定电话号码格式不正确";
				}
				if(consignee_id == "" && value.indexOf("*") > -1){
					errorFlag = true;
					errorMessage = "固定电话号码格式不正确";
				}
			}
		}
	}
	//yanwenqi 全球购增加身份证号
	// 验证身份证号码
	else if (divId == "idCard_div") {
		if(flowType == 10){
			value = $("#consignee_idCard").val();
			if (isEmpty(value)) {
				errorFlag = true;
				errorMessage = "请您填写身份证号码";
			} else {
				//consignee_id为""，说明是新建地址，新建地址不能有*
				if(consignee_id == "" && value.indexOf("*") > -1){
					errorFlag = true;
					errorMessage = "身份证号码格式不正确";
				}
				//新增的地址判断身份证是否合法
				if(consignee_id == ""){
					if (!check_idCard(value)) {
						errorFlag = true;
						errorMessage = "身份证号码格式不正确";
					}
				}else{
					if(!check_idCard_new(value)){
						errorFlag = true;
						errorMessage = "身份证号码格式不正确";
					}
				}
			}
		}
	}
	//end
	// 验证电话号码
	else if (divId == "call_phone_div") {
		value = $("#consignee_phone").val();
		divId = "call_div";
		if (!isEmpty(value)) {
			if (!is_forbid_new(value)) {
				errorFlag = true;
				errorMessage = "固定电话号码中含有非法字符";
			}
			if (!checkPhoneNew($("#consignee_mobile").val(),value)) {
				errorFlag = true;
				errorMessage = "固定电话号码格式不正确";
			}
			if(consignee_id == "" && value.indexOf("*") > -1){
				errorFlag = true;
				errorMessage = "固定电话号码格式不正确";
			}
		}
		if (true) {
			value = $("#consignee_mobile").val();
			if (isEmpty(value)) {
				errorFlag = true;
				errorMessage = "请您填写收货人手机号码";
			} else {
				if (!check_mobile_new(value)) {
					errorFlag = true;
					errorMessage = "手机号码格式不正确";
				}
				if(consignee_id == "" && value.indexOf("*") > -1){
					errorFlag = true;
					errorMessage = "手机号码格式不正确";
				}
			}
		}
	}
	
	if (errorFlag) {
		$("#" + divId + "_error").html(errorMessage);
		$("#" + divId + "_error").addClass("message");
		return false;
	} else {
		$("#" + divId + "_error").removeClass("message");
		$("#" + divId + "_error").html("");
	}
	return true;
}

/**
 * 删除收货人验证提示信息
 */
function removeConsingeeMessage() {
	$("#name_div_error").removeClass("message");
	$("#area_div_error").removeClass("message");
	$("#address_div_error").removeClass("message");
	$("#call_div_error").removeClass("message");
	$("#email_div_error").removeClass("message");
	//yanwenqi 全球购添加身份证号
	$("#idCard_div_error").removeClass("message");
	//end
	$("#name_div_error").html("");
	$("#area_div_error").html("");
	$("#address_div_error").html("");
	$("#call_div_error").html("");
	$("#email_div_error").html("");
	//yanwenqi 全球购添加身份证号
	$("#idCard_div_error").html("");
	//end
}

/**
 * 获取省份列表
 */
function loadProvinces() {
	var actionUrl = OrderAppConfig.DynamicDomain + "/consignee/getProvinces.action";
	jQuery.ajax({
		type : "POST",
		dataType : "text",
		url : actionUrl,
		data : null,
		cache : false,
		success : function(dataResult, textStatus) {
			// 没有登录跳登录
			if (parent.isUserNotLogin(dataResult)) {
				parent.goToLogin();
				return;
			}
			if (parent.isHasMessage(dataResult)) {
				var message = parent.getMessage(dataResult);
				parent.showMessageWarn(message);
			} else {
				$("#span_province").html(dataResult);
			}
			tabIndex();
		},
		error : function(XMLHttpResponse) {
			//parent.showMessageWarn("系统繁忙，请稍后再试！");
			return false;
		}
	});
}

/**
 * 获取城市列表
 */
function loadCitys() {
	var provinceId = $("#consignee_province").find("option:selected").val();
	var provinceName = isEmpty(provinceId) ? "" : $("#consignee_province").find("option:selected").text().replace("*", "");
	$("#areaNameTxt").html(provinceName);
	if (provinceId == null || provinceId == "") {
		$("#span_city").html("<select class='selt' id=\"consignee_city\" name=\"consigneeParam.city\"><option selected=\"\" value=\"\">请选择：</option></select>");
		$("#span_county").html("<select class='selt' id=\"consignee_county\" name=\"consigneeParam.countyId\"><option selected=\"\" value=\"\">请选择：</option></select>");
		$("#span_town").html("");
		return;
	}
	var param = "consigneeParam.provinceId=" + provinceId;
	var actionUrl = OrderAppConfig.DynamicDomain + "/consignee/getCitys.action";
	jQuery.ajax({
		type : "POST",
		dataType : "text",
		url : actionUrl,
		data : param,
		cache : false,
		success : function(dataResult, textStatus) {
			// 没有登录跳登录
			if (parent.isUserNotLogin(dataResult)) {
				parent.goToLogin();
				return;
			}
			if (parent.isHasMessage(dataResult)) {
				var message = parent.getMessage(dataResult);
				parent.showMessageWarn(message);
			} else {
				$("#span_city").html(dataResult);
				$("#span_county").html(
						"<select class='selt' id=\"consignee_county\" name=\"consigneeParam.countyId\"><option selected=\"\" value=\"\">请选择：</option></select>");
				$("#span_town").html("");
			}
			tabIndex();
			$("#consignee_city").focus();
		},
		error : function(XMLHttpResponse) {
			//parent.showMessageWarn("系统繁忙，请稍后再试！");
			return false;
		}
	});
}

/**
 * 获取县级列表
 */
function loadCountys() {
	var cityId = $("#consignee_city").find("option:selected").val();
	var provinceName = $("#consignee_province").find("option:selected").text().replace("*", "");
	var cityName = isEmpty(cityId) ? "" : $("#consignee_city").find("option:selected").text().replace("*", "");
	$("#areaNameTxt").text(provinceName + cityName);
	var param = "consigneeParam.cityId=" + cityId;
	var actionUrl = OrderAppConfig.DynamicDomain + "/consignee/getCountys.action";
	jQuery.ajax({
		type : "POST",
		dataType : "text",
		url : actionUrl,
		data : param,
		cache : false,
		success : function(dataResult, textStatus) {
			// 没有登录跳登录
			if (parent.isUserNotLogin(dataResult)) {
				parent.goToLogin();
				return;
			}
			if (parent.isHasMessage(dataResult)) {
				var message = parent.getMessage(dataResult);
				parent.showMessageWarn(message);
			} else {
				if (dataResult != null) {
					$("#span_county").html(dataResult);
					$("#span_town").html("");
				}
			}
			tabIndex();
			$("#consignee_county").focus();
			
		},
		error : function(XMLHttpResponse) {
			//parent.showMessageWarn("系统繁忙，请稍后再试！");
			return false;
		}
	});
}

/**
 * 获取乡镇街道列表
 */
function loadTowns() {
	var countyId = $("#consignee_county").find("option:selected").val();
	var provinceName = $("#consignee_province").find("option:selected").text().replace("*", "");
	var cityName = $("#consignee_city").find("option:selected").text().replace("*", "");
	var countyName = isEmpty(countyId) ? "" : $("#consignee_county").find("option:selected").text().replace("*", "");
	$("#areaNameTxt").text(provinceName + cityName + countyName);

	var param = "consigneeParam.countyId=" + countyId;
	var actionUrl = OrderAppConfig.DynamicDomain + "/consignee/getTowns.action";
	jQuery.ajax({
		type : "POST",
		dataType : "text",
		url : actionUrl,
		data : param,
		cache : false,
		success : function(dataResult, textStatus) {
			// 没有登录跳登录
			if (parent.isUserNotLogin(dataResult)) {
				parent.goToLogin();
				return;
			}
			if (parent.isHasMessage(dataResult)) {
				var message = parent.getMessage(dataResult);
				parent.showMessageWarn(message);
			} else {
				if (dataResult != null && dataResult != "area_empty") {
					$("#span_town").html(dataResult);
					$("#span_town").show();
				} else {
					$("#span_town").html("");
					$("#span_town").hide();
				}
			}
			tabIndex();
			$("#consignee_town").focus();
			//yanwenqi 自提地址项目  在编辑自提地址的时候，地址联动时，异步去后台调用推荐自提地址列表
			if($("#isEditSelfPickHtml").length>0){
				var townName = $("#consignee_town").find("option:selected").text().replace("*", "");
				if(townName != "请选择："){
					doEditPickFourLevelAddress();
				}
			}
		},
		error : function(XMLHttpResponse) {
			//parent.showMessageWarn("系统繁忙，请稍后再试！");
			return false;
		}
	});
}

function setTownName() {
	var provinceName = $("#consignee_province").find("option:selected").text().replace("*", "");
	var cityName = $("#consignee_city").find("option:selected").text().replace("*", "");
	var countyName = $("#consignee_county").find("option:selected").text().replace("*", "");
	var townName = $("#consignee_town").find("option:selected").text().replace("*", "");

	var provinceId = $("#consignee_province").find("option:selected").val();
	var cityId = $("#consignee_city").find("option:selected").val();
	var countyId = $("#consignee_county").find("option:selected").val();
	var townId = $("#consignee_town").find("option:selected").val();
	$("#areaNameTxt").text(provinceName + cityName + countyName + townName);
	tabIndex();
	
	//yanwenqi 自提 地址项目 
	if($("#isEditSelfPickHtml").length>0){
		doEditPickFourLevelAddress();
	}
}

function tabIndex(){
	$("#consignee_name").attr("tabindex","1");
	$("#consignee_province").attr("tabindex","2");
	$("#consignee_city").attr("tabindex","3");
	$("#consignee_county").attr("tabindex","4");
	$("#consignee_town").attr("tabindex","5");
	$("#consignee_address").attr("tabindex","6");
	$("#consignee_mobile").attr("tabindex","7");
	$("#consignee_phone").attr("tabindex","8");
	$("#consignee_email").attr("tabindex","9");
}


//根据四级地址获取可用的自提点
window.siteScroll = null;
function doEditPickFourLevelAddress(obj,consigneeAddr) {
//	var s = "<div class='c-site-item'><label><div class='c-site-r'><input type='radio' name='pickSiteId' value='111'></div><div class='c-site-name'>北京通州自提点<b></b></div></label><div class='c-site-field'>";
//		s = s+"<div class='c-site-field-title'><span>地址：</span></div><div class='c-site-field-detail'><span class='tip'>北京市通州区梨园南街华业东方玫瑰底商D7812一层022-27237176</span></div><div class='c-site-field-a'><a href='#none' class='ftx-05 map-link'>详细地图</a></div></div><div class='clr'></div></div>";
	$(".ui-scrollbar-main").html("");
	var isEditOrUse = $("#isEditOrUse").val();
	var pickid = -1;
	if(isEditOrUse==0){
		pickid  = parent.$("#pickName").attr("pickid");
	}else if(isEditOrUse==1){
		pickid  = parent.$("#recommendAddr").attr("pickid");
	}
  var actionUrl = OrderAppConfig.AsyncDomain + "/payAndShip/getMorePickSiteListAndByAddress.action";
  var payId = parent.$("#payment-list .payment-item.item-selected").attr("payid");
//  var provinceId = $("#consignee_province").find("option:selected").val();
//  var cityId = $("#consignee_city").find("option:selected").val();
//  var countyId = $("#consignee_county").find("option:selected").val();
//  var townId = $("#consignee_town").find("option:selected").val();
  var provinceId = null;
  var cityId = null;
  var countyId = null;
  var townId = null;
	if(obj==1){
		provinceId = $("#hidden_consignee_provinceId_").val();
		cityId = $("#hidden_consignee_cityId_").val();
		countyId = $("#hidden_consignee_countyId_").val();
		townId = $("#hidden_consignee_townId_").val();
	}else{
		provinceId = consigneeArea.provinceId;
		cityId = consigneeArea.cityId;
		countyId = consigneeArea.countyId;
		townId = consigneeArea.townId;
	}
	
//	consignee_provinceName = consigneeArea.provinceName;
//	consignee_cityName = consigneeArea.cityName;
//	consignee_countyName = consigneeArea.countyName;
//	consignee_townName = consigneeArea.townName;
  if (isEmpty(payId)) {
    payId = 4;
  }
//  var regionId = $(obj).val();
//  var regionIds = parent.$(obj).val();
//  if (isEmpty(regionId)) {
//    regionId = "-1";
//  }
//  $("#temp_pick_sel_regionid").val(regionId);
  //var repRegionId = regionId.replace(/:/g,"-");
  var param = "shipParam.payId=" + payId;
  param = param + "&shipParam.provinceId=" + provinceId;
  param = param + "&shipParam.cityId=" + cityId;
  param = param + "&shipParam.countyId=" + countyId;
  param = param + "&shipParam.townId=" + townId;
  param = param + "&shipParam.pickSiteNum=" + 50;
  param = parent.addFlowTypeParam(param);
  
  if(consigneeAddr!=null && consigneeAddr != "undefined" && consigneeAddr != ""){
	  param = param + "&shipParam.consigneeAddr="+consigneeAddr;
  }
  
  jQuery.ajax({
    type: "POST",
    dataType: "json",
    url: actionUrl,
    data: param,
    cache: false,
    success: function(dataResult, textStatus) {
      // 没有登录跳登录
//      if (isUserNotLogin(dataResult)) {
//        goToLogin();
//        return;
//      }
      if (dataResult == "null") {
        alert("自提点获取异常，请重新选择或稍后尝试");
//        goOrder();
      }
      var jsonO = dataResult;
      var pickSiteListHTML = "";
      if (jsonO.pickViewList.length == 0) {
    	  $(".c-pick-site-container").addClass("hide");
    	  $(".c-pick-site-container.no-site").removeClass("hide");
      }
      if (jsonO.pickViewList.length != 0) {
    	  $(".c-pick-site-container").removeClass("hide");
    	  $(".c-pick-site-container.no-site").addClass("hide");
    	  
    	  var checkFlag = 0;
        for (var i = 0; i < jsonO.pickViewList.length; i++) {
          pickSiteListHTML = pickSiteListHTML + "<div class='c-site-item ";
//          if (jsonO.pickViewList[i].selected == true) {
          if (jsonO.pickViewList[i].cabinetAvailable == false) {
            pickSiteListHTML = pickSiteListHTML + " c-site-item-disabled";
            pickSiteListHTML = pickSiteListHTML + "'><label><div class='c-site-r'><input type='radio' name='pickSiteId' disabled value='";
          }else{
        	pickSiteListHTML = pickSiteListHTML + "'><label><div class='c-site-r'><input type='radio' name='pickSiteId' value='";
          }
          
          pickSiteListHTML = pickSiteListHTML + jsonO.pickViewList[i].pickId;
          pickSiteListHTML = pickSiteListHTML + "' pickSiteName='";
          pickSiteListHTML = pickSiteListHTML + jsonO.pickViewList[i].pickName;
          pickSiteListHTML = pickSiteListHTML + "'";
          if(pickid == jsonO.pickViewList[i].pickId && jsonO.pickViewList[i].cabinetAvailable != false){
              pickSiteListHTML = pickSiteListHTML + "checked='checked'";
              checkFlag = 1;
          }else if (checkFlag == 0 && jsonO.pickViewList[i].cabinetAvailable != false) {
              pickSiteListHTML = pickSiteListHTML + "checked='checked'";
              checkFlag = 1;
          }
          pickSiteListHTML = pickSiteListHTML + "></div><div class='c-site-name'>";
          pickSiteListHTML = pickSiteListHTML + jsonO.pickViewList[i].pickName;
          if (jsonO.pickViewList[i].cabinetAvailable == false) {
              pickSiteListHTML = pickSiteListHTML + "<span class='ftx-01'>[已满]</span>";
            }
          pickSiteListHTML = pickSiteListHTML + "<b></b></div></label><div class='c-site-field'><div class='c-site-field-title'><span></span></div><div class='c-site-field-detail'><span class='tip'>";
          pickSiteListHTML = pickSiteListHTML + jsonO.pickViewList[i].address;
          pickSiteListHTML = pickSiteListHTML + "</span></div><div class='c-site-field-a' style='margin-left:30px'><a target='_blank' href='";
          pickSiteListHTML = pickSiteListHTML + jsonO.pickViewList[i].mapUrl + "' class='ftx-05 map-link'>详细地图</a></div></div><div class='clr'></div></div>";
         
        }
        
      }
      $(".ui-scrollbar-main").html(pickSiteListHTML);
    	  if(siteScroll) {
    		  siteScroll.resetUpdate($('.c-pick-sites .ui-scrollbar-main .c-site-item').length);
    	  } else {
    		  if($('.c-pick-sites .ui-scrollbar-main').height() > 240){
    		  //if($('.c-pick-sites .ui-scrollbar-main .c-site-item').length>4){
    		    seajs.use(['jdf/1.0.0/ui/scrollbar/1.0.0/scrollbar' ,
    		               'jdf/1.0.0/ui/drag/1.0.0/drag'],function(){ 
    		                	  siteScroll = $('.c-pick-sites').scrollbar({ 
    		                      width: 11,
    		                      scrollClass: 'ui-scrollbar-item-consignee', 
    		                      mainClass: 'ui-scrollbar-main',
    		                      limit: true,
    		                      hasHeadTail:false,
    		                      wrapHeight: 240
    		              });
    		                	  
    		                  // 组件update方法，不能隐藏背景图，此处建议后端开发每次变更自提点区域重新初始化滚动条
    		                	  siteScroll.resetUpdate = function(length) {
    		          		        var _height = (length * 60);
    		          		        $('.c-pick-sites .ui-scrollbar-main').css({'height': _height+"px"});
    		          		        if(_height > 240) {
    		          		          $('.ui-scrollbar-wrap').css({'height': '240px'});
    		          		        } else {
    		          		          $('.ui-scrollbar-wrap').css({'height': _height+"px"});
    		          		        }
    		          		      if($('.c-pick-sites .ui-scrollbar-main').height() > 240){
    		          		        $('.ui-scrollbar-bg').css({'display': 'block', 'top': '0'});
    		          		        $('.c-pick-sites').css('position', 'absolute');
    		          		      }else{
    		          		        $('.ui-scrollbar-bg').css('display', 'none');
    		          		        $('.c-pick-sites').css({'height': '', 'position': ''});
    		          		      }  
    		          		      this.update();
    		          		  };
    		              });
    	  	}
    	  }

    },
    error: function(XMLHttpResponse) {
      alert("系统繁忙，请稍后再试！");
      return false;
    }
  });
}

/**
 * yanwenqi 自提地址项目 取消编辑
 */
function cancel_SelfPickConsignee() {
	parent.$.closeDialog();
}

/**
 * yanwenqi 自提地址项目 保存自提点
 * 保存收货地址（包含保存常用人收货地址，根据id区分）
 */
function save_SelfPickConsignee(fromRecommend) {
	// 如果不隐藏重新获取用户填写的信息
	var consignee_provinceId = null;
	var consignee_cityId = null;
	var consignee_countyId = null;
	var consignee_townId = null;
	var consignee_name = null;
	var consignee_mobile = null;
	var consignee_phone = null;
	var consignee_provinceName = null;
	var consignee_cityName = null;
	var consignee_countyName = null;
	var consignee_townName = null;
	var consignee_pickId = null; 
	var consignee_pickName = null;
	var fromRecommends = 0;
	if(fromRecommend==1){
		fromRecommends = 1;
	}
	// 使用地区选择tab的情况
	consignee_provinceId = consigneeArea.provinceId;
	consignee_cityId = consigneeArea.cityId;
	consignee_countyId = consigneeArea.countyId;
	consignee_townId = consigneeArea.townId;
	consignee_provinceName = consigneeArea.provinceName;
	consignee_cityName = consigneeArea.cityName;
	consignee_countyName = consigneeArea.countyName;
	consignee_townName = consigneeArea.townName;
//	consignee_provinceId = $("#consignee_province").find("option:selected").val();
//	consignee_cityId = $("#consignee_city").find("option:selected").val();
//	consignee_countyId = $("#consignee_county").find("option:selected").val();
//	consignee_townId = $("#consignee_town").find("option:selected").val();
//	consignee_provinceName = $("#consignee_province").find("option:selected").text();
//	consignee_cityName = $("#consignee_city").find("option:selected").text();
//	consignee_countyName = $("#consignee_county").find("option:selected").text();
//	if (!$("#span_town").is(":hidden"))
//		consignee_townName = $("#consignee_town").find("option:selected").text();
	
	consignee_name = $("#consignee_name").val();
	consignee_mobile = $("#consignee_mobile").val();
	consignee_phone = $("#consignee_phone").val();
	consignee_pickId = $('input[name="pickSiteId"]:checked').val();
	
	consignee_pickName = $('input[name="pickSiteId"]:checked').attr("pickSiteName");

	var addr = $('input[name="pickSiteId"]:checked').parent().parent().next().find('.c-site-field-detail').find('.tip').text();
	if($('input[name="pickSiteId"]:checked').length==0){
		return;
	}
	if (consignee_phone == null || consignee_phone == "undefined")
		consignee_phone = "";
	var checkConsignee = true;
	// 验证收货人信息是否正确
	if (!check_SelfConsignee("selfname_div"))
		checkConsignee = false;
	// 验证地区是否正确
	if (!check_SelfConsignee("selfarea_div"))
		checkConsignee = false;
	// 验证手机号码是否正确
	if (!check_SelfConsignee("call_selfmobile_div"))
		checkConsignee = false;	
	// 验证电话是否正确
	if (!check_SelfConsignee("call_selfphone_div"))
		checkConsignee = false;
	if (!checkConsignee)
		return;	
	consignee_provinceName = consignee_provinceName.replace("*", "");
	consignee_cityName = consignee_cityName.replace("*", "");
	consignee_countyName = consignee_countyName.replace("*", "");
//	if (isEmpty(consignee_townId) || $("#span_town").is(":hidden"))
//		consignee_townId = 0;
//	if (isEmpty(consignee_townId))
//		consignee_townId = 0;
	var areaName = consignee_provinceName;
		areaName = areaName + consignee_cityName;
//	    areaName = areaName +" ";
	    areaName = areaName + consignee_countyName;
	    if(!isEmpty(consignee_townName)){
//	    	areaName = areaName +" ";
	    	areaName = areaName + consignee_townName;
	    }
	var param = "consigneeParam.name=" + consignee_name + "&consigneeParam.provinceId=" + consignee_provinceId 
		+ "&consigneeParam.cityId=" + consignee_cityId + "&consigneeParam.countyId=" + consignee_countyId 
		+ "&consigneeParam.townId=" + consignee_townId + "&consigneeParam.pickId=" + consignee_pickId
		+ "&consigneeParam.mobile=" + consignee_mobile + "&consigneeParam.phone=" + consignee_phone
		+ "&consigneeParam.provinceName="+consignee_provinceName+"&consigneeParam.cityName="+consignee_cityName
		+ "&consigneeParam.countyName="+consignee_countyName+"&consigneeParam.townName="+consignee_townName
		+ "&consigneeParam.showMessage="+addr+ "&consigneeParam.fromRecommend="+fromRecommends;
	param = parent.addFlowTypeParam(param);
	var actionUrl = OrderAppConfig.DynamicDomain + "/consignee/saveSelfPick.action";
	jQuery.ajax({
		type: "POST",
		dataType: "text",
		url: actionUrl,
		data: param,
		cache: false,
		success: function(dataResult, textStatus) {
			  parent.$("#defaultSelfPick").remove();
			  var showMobile = consignee_mobile.substring(0,3) + "****";
			  showMobile = showMobile + consignee_mobile.substring(7,11);
	    	  var pickSiteListHTML = "";
	    	  pickSiteListHTML = pickSiteListHTML + "<li id='defaultSelfPick' defaultSelfPick='1'><div id='selfPickSiteName' class='consignee-item item-selected' limit='8'";
	    	  pickSiteListHTML = pickSiteListHTML + "onclick=doSelectSelfPickSite('";
	    	  pickSiteListHTML = pickSiteListHTML + consignee_pickId;
	    	  pickSiteListHTML = pickSiteListHTML + "','";
	    	  pickSiteListHTML = pickSiteListHTML + consignee_pickName;
	          pickSiteListHTML = pickSiteListHTML + "')><span id='pickName' pickId='";
	    	  pickSiteListHTML = pickSiteListHTML + consignee_pickId;
	    	  pickSiteListHTML = pickSiteListHTML + "'>";
	    	  pickSiteListHTML = pickSiteListHTML + "京东自提点";
	    	  pickSiteListHTML = pickSiteListHTML + "</span><b></b></div><div class='addr-detail'><span class='addr-name' limit='6'>";
	    	  pickSiteListHTML = pickSiteListHTML + consignee_name;
	    	  pickSiteListHTML = pickSiteListHTML + "</span><span class='addr-info' limit='45'>";
	    	  pickSiteListHTML = pickSiteListHTML + consignee_pickName+" ";
//	    	  pickSiteListHTML = pickSiteListHTML + areaName+" ";
//	    	  pickSiteListHTML = pickSiteListHTML + " ";
//	    	  pickSiteListHTML = pickSiteListHTML + addr.substring(3);
	    	  pickSiteListHTML = pickSiteListHTML + addr;
	    	  pickSiteListHTML = pickSiteListHTML + "</span><span class='addr-tel'>";
	    	  pickSiteListHTML = pickSiteListHTML + showMobile;
	    	  pickSiteListHTML = pickSiteListHTML + "</span></div><div class='addr-ops'>";
	    	  pickSiteListHTML = pickSiteListHTML + "<a href='#none' onclick='openEditSelfPickConsigneeDialog()' class='ftx-05 mr10 edit-selfconsignee' fid='";
	    	  pickSiteListHTML = pickSiteListHTML + consignee_pickId;
	    	  pickSiteListHTML = pickSiteListHTML + "'>更换自提地址</a>";
	    	  pickSiteListHTML = pickSiteListHTML + "</div></li>";
	    	  parent.$("#selfPickInfo").append(pickSiteListHTML);
	    	  parent.subStrConsignee();
	    	  //将自提点的值改了
	    	    var pickId = consignee_pickId;
	    	    var regionId = $("#pick_sel_regionid").val();//这里的regionId要不要改？？？？
	    	    var pick_name = consignee_pickName;
	    	    if (pick_name == "null" || pick_name == null || pick_name == "undefined" || pick_name == undefined) {
	    	      pick_name = "";
	    	    }
	    	    var showPickSite = "<span class='ftx-03'>自提地点：</span>" + pick_name;
	    	    //如果没有选中自提点，不与保存
	    	    if (isEmpty(pickId)) {
	    	      return;
	    	    }
	    	    $("#beforePickRegionId").val(regionId);
	    	    $("#beforePickSelRegionid").val(regionId);
	    	    $("#beforePickSiteId").val(pickId);
	    	    $("#beforePickName").val(pick_name);

	    	    $("#pick_sel_regionid").val(regionId);
	    	    $("#pick_sel_id").val(pickId);
	    	    $("#is_invoke_pickdate").val("1");

	    	    $("#selfpick_name").html(showPickSite);
	    	    $("#saveParam_pickSiteId").val(pickId);
	    	    $("#saveParam_pickRegionId").val(regionId);
	    	    $("#saveParam_pickShipmentType").val(64);
	    	    $("#saveParam_jdShipmentType").val("");
	    	    $("#saveParam_otherShipmentType").val("");
	    	  //将自提点的值改了end
			  parent.doSelectSelfPickSite(consignee_pickId,consignee_pickName)
			  
			setTimeout(function(){
				window.location.href = "about:blank";
				parent.$.closeDialog();
			});
			},
		error: function(XMLHttpResponse) {
			console.log(XMLHttpResponse);
			parent.$.closeDialog();
		}
	});
}


/**
 * yanwenqi 自提地址项目
 * 验证收货地址消息提示
 * 
 * @param divId
 * @param value
 */
function check_SelfConsignee(divId) {
  var errorFlag = false;
  var errorMessage = null;
  var value = null;
  // 验证收货人名称
  if (divId == "selfname_div") {
    value = $("#consignee_name").val();
    if (isEmpty(value)) {
      errorFlag = true;
      errorMessage = "请填写收货人姓名";
    }
    if (value.length > 25) {
      errorFlag = true;
      errorMessage = "姓名不能大于25位";
    }
    if (!is_forbid(value)) {
      errorFlag = true;
      errorMessage = "姓名中含有非法字符";
    }
  }
  // 验证地区是否完整
  else if (divId == "selfarea_div") {
		var provinceId = consigneeArea.provinceId;
		var cityId = consigneeArea.cityId;
		var countyId = consigneeArea.countyId;
		var townId = consigneeArea.townId;
		// 验证地区是否正确
		if ("0" == provinceId || "0" == cityId || "0" == countyId
				|| 0 == provinceId || 0 == cityId || 0 == countyId) {
			errorFlag = true;
			errorMessage = "请您填写完整的地区信息";
		}
  }
  // 验证手机号码
  else if (divId == "call_selfmobile_div") {
    value = $("#consignee_mobile").val();
    if (isEmpty(value)) {
      errorFlag = true;
      errorMessage = "请填写手机号码";
    } else {
      if (!check_mobile_new(value)) {
        errorFlag = true;
        errorMessage = "手机号码格式不正确";
      }
    }
  }
  // 验证电话号码
  else if (divId == "call_selfphone_div") {  	  
    value = $("#consignee_phone").val();
    if (!isEmpty(value)) {
      if (!is_forbid_new(value)) {
        errorFlag = true;
        errorMessage = "固定电话号码中含有非法字符";
      }
    }
  }
  if (errorFlag) {
    $("#" + divId + "_error").html(errorMessage);
    $("#" + divId + "_error").addClass("message");
    return false;
  } else {
    $("#" + divId + "_error").removeClass("message");
    $("#" + divId + "_error").html("");
  }
  return true;
}window.check_SelfConsignee=check_SelfConsignee;

/**
 * 手机号中间4位为*，所以去除*的校验
 * @param temp_str
 * @returns {Boolean}
 */
function is_forbid_new(temp_str){
    temp_str = trimTxt(temp_str);
	temp_str = temp_str.replace('--',"@");
	temp_str = temp_str.replace('/',"@");
	temp_str = temp_str.replace('+',"@");
	temp_str = temp_str.replace('\'',"@");
	temp_str = temp_str.replace('\\',"@");
	temp_str = temp_str.replace('$',"@");
	temp_str = temp_str.replace('^',"@");
	temp_str = temp_str.replace('.',"@");
	temp_str = temp_str.replace(';',"@");
	temp_str = temp_str.replace('<',"@");
	temp_str = temp_str.replace('>',"@");
	temp_str = temp_str.replace('"',"@");
	temp_str = temp_str.replace('=',"@");
	temp_str = temp_str.replace('{',"@");
	temp_str = temp_str.replace('}',"@");
	var forbid_str=new String('@,%,~,&');
	var forbid_array=new Array();
	forbid_array=forbid_str.split(',');
	for(i=0;i<forbid_array.length;i++){
		if(temp_str.search(new RegExp(forbid_array[i])) != -1)
		return false;
	}
	return true;
}

/**
 * 邮箱校验新规则
 * @param id
 * @returns {Boolean}
 */
function check_consignee_email(id){
	var errorFlag = false;
	var errorMessage ="";
	value = $("#"+id).val();
	if (!isEmpty(value)) {
		if (value.length > 50) {
			errorFlag = true;
			errorMessage = "邮箱长度不能大于50位";
		}
		if (!check_email_new(value)) {
			errorFlag = true;
			errorMessage = "邮箱格式不正确";
		}
	}
	if (errorFlag) {
		$("#email_div_error").html(errorMessage);
		$("#email_div").addClass("message");
		return false;
	} else {
		$("#email_div").removeClass("message");
		$("#email_div_error").html("");
	}
	return true;
}
function setEditAddressAilas(itemId){
	var alias = $("#"+itemId).text();
	$("input[id=consignee_addressName]").val(alias);
}

